<?php

require_once "services/FacebookProxy.php";
require_once "www/_include/smarty/libs/Smarty.class.php";
require_once "data/dao/UserDAO.php";
require_once "data/dao/GameDAO.php";
require_once "www/deleteUser.php";
require_once "conf.php";
require_once "application/game/Locale.php";
require_once "utils/HTTPRequest.php";

class AdminRenderer{
	
	private $user;
	private $facebook;
	private $loggedIn;
	private $smarty;
	
	public function AdminRenderer(){
	
		$this->facebook = new FacebookProxy();		
		$this->smarty = new Smarty();
		$this->user = $this->facebook->getLoggedInUser();
		$this->loggedIn = false;
		$password = HTTPRequest::getParam("password");
		
		if ($this->facebook->getLoggedInUser() == "535540211" &&
			  $password == ADMIN_PASSWORD){
			  	
			  	$this->loggedIn = true;
		}

		/*
		 * send email notification to users
		 */
		if(HTTPRequest::getParam("do") == "send-email"){
			
			$subject = HTTPRequest::getParam("subject");
			$body = HTTPRequest::getParam("message");
			$this->sendEmailNotification($subject, $body);
		}
		
		/*
		 * update users
		 */
		if(HTTPRequest::getParam("do") == "update-users")
			$this->updateUsers();
		
		/*
		 * add a game to database
		 */
		if(HTTPRequest::getParam("do") == "add-game"){

			$gameId = HTTPRequest::getParam("game-id");
			$gameTitle = HTTPRequest::getParam("game-title");
			$gameManufacturer = HTTPRequest::getParam("game-manufacturer");
			$gameReleaseDate = HTTPRequest::getParam("game-release-date");
			$gameImageUrl = HTTPRequest::getParam("game-image-url");
			$gameAmazonUrl = HTTPRequest::getParam("game-amazon-url");
			$locale = HTTPRequest::getParam("game-locale");
			$description = HTTPRequest::getParam("game-description");

			$this->addGame($gameId, $gameTitle, $gameManufacturer, $gameReleaseDate, 
				$gameImageUrl, $gameAmazonUrl, $locale, $description);
		}
		
     	$this->getPlaceholderValues();
	    $this->renderPage();
	}
	
	private function getPlaceholderValues(){
		
		$this->smarty->assign("loggedIn", $this->loggedIn);
		$this->smarty->assign("users", $this->getUsers());
		$this->smarty->assign("locales", Locale::getAllLocales());
	}
	
	private function renderPage(){
	
		$this->smarty->display("admin.tpl");
		
	}
	
	private function sendEmailNotification($subject, $body){
		
		$udao = new UserDAO();
		$users = $udao->getAllUsers();
		$users = implode(",", $users);
		
		$this->facebook->sendEmailNotification($users, $subject, $body);
	}
	
	private function getUsers(){
		
		$userNames = array();
		
		$udao = new UserDAO();
		$users = $udao->getAllUsers();

		$userInfo = $this->facebook->getFacebookUserInfo(implode(",", $users));

		foreach($userInfo as $u){

			array_push($userNames, $u['first_name'] . " " . $u['last_name'] . ", " . $u['has_added_app']);
		}
		return $userNames;
	}
	
	private function updateUsers(){
		
		$udao = new UserDAO();
		$users = $this->facebook->getFacebookUserInfo(implode(",", $udao->getAllUsers()));
		
		$ur = new UserRemover();
		
		foreach($users as $u){
			
			if($u['has_added_app'] == false)
				$ur->deleteUser($u['uid']);
		}		
	}
	
	private function addGame($id, $title, $manufacturer, $releaseDate, $imageUrl, 
		$amazonUrl, $locale, $description){
		
		$gdao = new GameDAO();
		try{
			$gdao->addGame($id, $title, $manufacturer, $releaseDate, $imageUrl, 
				$amazonUrl, $locale, $description);
		}
		catch(Exception $e){
			echo "Error adding game to database!";
		}
	}
}
$ar = new AdminRenderer();
?>
